<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\models\DisWorksInfo;
use \yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
/* @var $title string */
/* @var $id int */
$this->title = $title . '审核';
$this->params['breadcrumbs'][] = $this->title;
//$this->registerJsFile('@web/static/jquery.imgbox.pack.js');
$this->registerCss("
/* Thumbnails */
a.imgbox {
  cursor: url(\"".Url::to('@web/statics/img/zoomIn.png')."\"), -moz-zoom-in;
}

/* Throbber */
.imgbox-loading::after {
  content: \" \";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: url(\"".Url::to('@web/statics/img/imgbox-spinner.gif')."\") center center no-repeat;
  cursor: pointer;
  z-index: 999;
}
");
$this->registerCssFile('@web/statics/css/imgbox.css');
?>
<section class="wrapper">
    <?php
    if (Yii::$app->getSession()->hasFlash('success'))
    {
        echo \yii\bootstrap\Alert::widget([
            'options' => [
                'class' => 'bg-success', //这里是提示框的class
            ],
            'body' => Yii::$app->getSession()->getFlash('success'), //消息体
        ]);
    }
    ?>
    <section class="panel">
        <header class="panel-heading"><?= Html::encode($this->title) ?></header>
        <div class="panel-body">
            <div class="dis-works-info-index">
                <?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],
                        [
                            'label' => '比赛',
                            'format' => 'raw',
                            'value' => function ($data)
                            {
                                /* @var $data DisWorksInfo */
                                return $data->getWorksClass1()->one()['name'];
                            }
                        ],
                        [
                            'label' => '类型',
                            'format' => 'raw',
                            'value' => function ($data)
                            {
                                /* @var $data DisWorksInfo */
                                return $data->getWorksClass2()->one()['name'];
                            }
                        ],
                        [
                            'label' => '申报人',
                            'format' => 'raw',
                            'value' => function ($data)
                            {
                                /* @var $data DisWorksInfo */
                                return $data->getUser()->one()['username'];
                            }
                        ],
                        [
                            'label' => '附件',
                            'format' => 'raw',
                            'value' => function ($data)
                                {
                                    /* @var $data DisWorksInfo */
                                    $uploads = $data->getDisUploads()->all();
                                    if (!empty($uploads))
                                    {
                                        $html="";
                                        foreach ($uploads as $key=>$upload)
                                        {
                                            $up=json_decode($upload->urls,true);
                                            if($up['type']=='img')
                                            {
                                                $html.=Html::a(Html::img($up['url'],['width'=>100,'height'=>100]),$up['url'],['id'=>"img-pop-{$data->id}-{$key}"]);
                                                $this->registerJs("$(\"#img-pop-{$data->id}-{$key}\").imgbox();");
                                            }elseif($up['type']=='zip')
                                            {
                                                $html.=Html::a('附件下载'.$key,$up['url']);
                                            }
                                        }
                                        return $html;
                                    }else
                                    {
                                        return Html::tag('p','没有附件');
                                    }
                                }

                        ],
                        [
                            'label' => '评分',
                            'format' => 'raw',
                            'value' =>
                                function ($data)
                                {
                                    $this->registerJs("
                                    $('#rating-{$data->id}').on('rating.change', function () {
                                        var star=$('#rating-{$data->id}').val();
                                        $.ajax( {  
                                              url:'".Url::toRoute('ajax/rating')."',
                                              data:{  
                                                       'id' : {$data->id},  
                                                       'star' : star
                                             },  
                                               type:'get',  
                                               cache:false,  
                                               dataType:'json',  
                                               success:function(data) {  
                                                   if(data.msg =='true'){   
                                                       alert('修改成功！');
                                                   }else{  
                                                       view(data.msg);  
                                                     }  
                                                  },  
                                                  error : function() {  
                                                       alert('ajax更新失败，网络异常！');  
                                                  }  
                                             });
                                    });
                                    
                                    ");
                                    $admin_id=\Yii::$app->user->getId();
                                    $score=\backend\models\DisStars::find()->where(['admin_id'=>$admin_id,'works_id'=>$data->id])->one()['score'];
                                    if(!$score) $score=0;
                                    return \kartik\rating\StarRating::widget([
                                        'name' => "rating-{$data->id}",
                                        'value' => $score,
                                        'id'=>"rating-{$data->id}",
                                        'pluginOptions' => [
                                            'showClear' => false,
                                            'min' => 0,
                                            'max' => 4,
                                            'stars' => 4,
                                            'step' => 1,
                                            'size' => 'xs',
                                            'starCaptions' => [
                                                1 => '优秀奖',
                                                2 => '三等奖',
                                                3 => '二等奖',
                                                4 => '一等奖'
                                            ],
                                            'starCaptionClasses' => [
                                                1 => 'label label-danger',
                                                2 => 'label label-warning',
                                                3 => 'label label-info',
                                                4 => 'label label-success',
                                            ],
                                            'theme' => 'krajee-svg',
                                            'filledStar' => '<span class="krajee-icon krajee-icon-star"></span>',
                                            'emptyStar' => '<span class="krajee-icon krajee-icon-star"></span>',
                                        ],
                                    ]);
                                }
                        ],
                        [
                            'class' => 'yii\grid\ActionColumn', 'template' => '{view}'
                        ],
                        ],
                    ]); ?>
            </div>
        </div>
    </section>
</section>
